#include "selectfriendarea.h"
#include "selectfrienditem.h"
#include"selectfriendscrollarea.h"

#include <QPushbutton>


SelectFriendArea::SelectFriendArea(QWidget *parent)
    :QDialog(parent)
{
    layout=new QHBoxLayout();
    layout->setContentsMargins(15,15,15,15);
    layout->setSpacing(15);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(700,550);
    this->setLayout(layout);
    this->setWindowIcon(QIcon(":/Resources/windowIcon.png"));
    this->setWindowTitle("选择好友");
    // 设置浅色主题对话框样式
    this->setStyleSheet(
        "SelectFriendArea {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:1,"
        "               stop:0 #f8f9fa, stop:0.5 #e9ecef, stop:1 #dee2e6);"
        "   color: #495057;"
        "   border: 1px solid #ced4da;"
        "   border-radius: 16px;"
        "}"
        );
    //初始化我们的左侧区域
    InitLeft();
    //初始化右侧
    InitRight();
}

void SelectFriendArea::InitLeft()
{
    // 创建左侧容器
    leftContainer = new QWidget();
    leftContainer->setObjectName("leftContainer");
    leftMainLayout = new QVBoxLayout();
    leftMainLayout->setContentsMargins(0,0,0,0);
    leftMainLayout->setSpacing(10);

    // 添加左侧标题
    leftTitle = new QLabel("好友列表");
    leftTitle->setObjectName("leftTitle");
    leftTitle->setAlignment(Qt::AlignCenter);
    leftTitle->setFixedHeight(40);
    leftTitle->setStyleSheet(
        "QLabel#leftTitle {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 rgba(13, 110, 253, 0.15), stop:1 rgba(25, 135, 84, 0.15));"
        "   color: #0d6efd;"
        "   font-size: 16px;"
        "   font-weight: bold;"
        "   border-radius: 8px;"
        "   border: 1px solid rgba(13, 110, 253, 0.3);"
        "}"
        );

    //创建一个滚动区域
    leftScrollArea=new SelectFriendScrollArea();
    leftScrollArea->setObjectName("leftScrollArea");

    leftMainLayout->addWidget(leftTitle);
    leftMainLayout->addWidget(leftScrollArea);
    leftContainer->setLayout(leftMainLayout);

    // 设置左侧容器样式
    leftContainer->setStyleSheet(
        "QWidget#leftContainer {"
        "   background: #ffffff;"
        "   border: 1px solid #dee2e6;"
        "   border-radius: 12px;"
        "   padding: 10px;"
        "}"
        );

    layout->addWidget(leftContainer,1);
}

void SelectFriendArea::InitRight()
{

    rightContainer = new QWidget();
    rightContainer->setObjectName("rightContainer");

    rightMainLayout=new QVBoxLayout();
    rightMainLayout->setSpacing(15);
    rightMainLayout->setContentsMargins(15,15,15,15);

    // 标题区域
    titleWidget = new QWidget();
    titleWidget->setFixedHeight(50);
    QHBoxLayout* titleLayout = new QHBoxLayout();
    titleLayout->setContentsMargins(0,0,0,0);

    rightFlag=new QLabel();
    rightFlag->setText("已选联系人");
    rightFlag->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    rightFlag->setStyleSheet(
        "QLabel {"
        "   color: #495057;"
        "   font-size: 16px;"
        "   font-weight: bold;"
        "   background: transparent;"
        "}"
        );
    countLabel = new QLabel();
    countLabel->setText(QString("(%1)").arg(this->count));
    countLabel->setObjectName("countLabel");
    countLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    countLabel->setStyleSheet(
        "QLabel#countLabel {"
        "   color: #6c757d;"
        "   font-size: 14px;"
        "   background: transparent;"
        "}"
        );

    titleLayout->addWidget(rightFlag);
    titleLayout->addStretch();
    titleLayout->addWidget(countLabel);
    titleWidget->setLayout(titleLayout);

    // 右侧滚动区域
    rightScrollArea=new SelectFriendScrollArea();
    rightScrollArea->setObjectName("rightScrollArea");
    rightScrollArea->setMinimumHeight(200);

    // 按钮区域
    QWidget* buttonWidget = new QWidget();
    buttonWidget->setFixedHeight(60);
    rightBtnLayout=new QHBoxLayout();
    rightBtnLayout->setSpacing(15);
    rightBtnLayout->setContentsMargins(0,0,0,0);

    rightCancelBtn=new QPushButton();
    rightCancelBtn->setFixedSize(100,40);
    rightCancelBtn->setText("取消");
    rightCancelBtn->setObjectName("rightCancelBtn");

    rightFinishBtn=new QPushButton();
    rightFinishBtn->setFixedSize(100,40);
    rightFinishBtn->setText("完成");
    rightFinishBtn->setObjectName("rightFinishBtn");

    // 浅色主题按钮样式
    QString cancelButtonStyle =
        "QPushButton {"
        "   background: #6c757d;"
        "   border: none;"
        "   border-radius: 8px;"
        "   color: white;"
        "   font-weight: bold;"
        "   font-size: 14px;"
        "}"
        "QPushButton:hover {"
        "   background: #5a6268;"
        "}"
        "QPushButton:pressed {"
        "   background: #495057;"
        "}";

    QString finishButtonStyle =
        "QPushButton {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #0d6efd, stop:1 #198754);"
        "   border: none;"
        "   border-radius: 8px;"
        "   color: white;"
        "   font-weight: bold;"
        "   font-size: 14px;"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #0b5ed7, stop:1 #157347);"
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #0a58ca, stop:1 #146c43);"
        "}";

    rightCancelBtn->setStyleSheet(cancelButtonStyle);
    rightFinishBtn->setStyleSheet(finishButtonStyle);

    rightBtnLayout->addStretch();
    rightBtnLayout->addWidget(rightCancelBtn);
    rightBtnLayout->addWidget(rightFinishBtn);

    buttonWidget->setLayout(rightBtnLayout);

    // 组装右侧布局
    rightMainLayout->addWidget(titleWidget);
    rightMainLayout->addWidget(rightScrollArea);
    rightMainLayout->addWidget(buttonWidget);

    rightContainer->setLayout(rightMainLayout);

    // 设置右侧容器样式
    rightContainer->setStyleSheet(
        "QWidget#rightContainer {"
        "   background: #ffffff;"
        "   border: 1px solid #dee2e6;"
        "   border-radius: 12px;"
        "}"
        );

    layout->addWidget(rightContainer,1);
}

void SelectFriendArea::addItem(const QString &userid, const QIcon &avator, const QString &text, bool checked)
{
    SelectFriendItem*item=new SelectFriendItem(leftScrollArea,userid,avator,text,checked);
    QVBoxLayout*left=dynamic_cast<QVBoxLayout*>(leftScrollArea->getContain()->layout());
    left->addWidget(item);
}

void SelectFriendArea::addSelectedItem(const QString &userid, const QIcon &avator, const QString &text, bool checed)
{
    SelectFriendItem*item=new SelectFriendItem(rightScrollArea,userid,avator,text,checed);
    QVBoxLayout*right=dynamic_cast<QVBoxLayout*>(rightScrollArea->getContain()->layout());
    right->addWidget(item,0,Qt::AlignTop);
    count=right->count();
    countLabel->setText(QString("%1").arg(count));
}

void SelectFriendArea::deleteSelectedItem(const QString &userid)
{
    QVBoxLayout*right=dynamic_cast<QVBoxLayout*>(rightScrollArea->getContain()->layout());
    for(int i=right->count();i>=0;i--)
    {
        QLayoutItem*item=right->itemAt(i);
        if(item&&item->widget())
        {
            SelectFriendItem*fri=dynamic_cast<SelectFriendItem*>(item->widget());
            if(fri&&fri->userID()==userid)
            {
                right->removeWidget(fri);
                //点击右侧删除的时候此时便利到右侧我们还正在使用这个控件不能立马删除
                fri->deleteLater();
                break;
            }
        }
    }
    //删除之后保证右侧的layout对应的元素的设置为未选中状态
    QVBoxLayout*left=dynamic_cast<QVBoxLayout*>(leftScrollArea->getContain()->layout());
    for(int i=0;i<left->count();i++)
    {
        QLayoutItem*item=left->itemAt(i);
        if(item&&item->widget())
        {
            SelectFriendItem*fri=dynamic_cast<SelectFriendItem*>(item->widget());
            if(fri&&fri->userID()==userid)
            {
                fri->getCheckBox()->setChecked(false);
                break;
            }
        }

    }
    count=right->count();
    countLabel->setText(QString("%1").arg(count));

}

SelectFriendScrollArea *SelectFriendArea::getLeftScroll()
{
    return this->leftScrollArea;
}

SelectFriendScrollArea *SelectFriendArea::getRightScroll()
{
    return this->rightScrollArea;
}


